package handler

import (
	"oa-system/internal/api/request"
	"oa-system/internal/service"
	"oa-system/pkg/errors"
	"oa-system/pkg/response"
	"oa-system/pkg/utils"

	"github.com/gin-gonic/gin"
)

type DepartmentHandler struct {
	departmentService service.DepartmentService
}

func NewDepartmentHandler(departmentService service.DepartmentService) *DepartmentHandler {
	return &DepartmentHandler{
		departmentService: departmentService,
	}
}

// GetAllDepartments godoc
// @Summary 获取部门列表
// @Description 获取所有部门信息，支持分页和名称搜索
// @Tags 部门模块
// @Accept json
// @Produce json
// @Param page query int false "当前页码" default(1)
// @Param pageSize query int false "每页记录数" default(10)
// @Param name query string false "部门名称模糊搜索"
// @Success 200 {object} response.Response{data=response.PageData{list=[]model.Department,total=int}}
// @Router /departments [get]
func (h *DepartmentHandler) GetAllDepartments(c *gin.Context) {
	var req request.DepartmentListRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.Error(c, errors.NewBadRequestError("无效的请求参数", err))
		return
	}

	departments, total, err := h.departmentService.GetAllDepartments(req.Page, req.PageSize, req.Name)
	if err != nil {
		response.Error(c, err)
		return
	}

	response.PageSuccess(c, departments, total, req.Page, req.PageSize)
}

// CreateDepartment godoc
// @Summary 创建部门
// @Description 创建新部门
// @Tags 部门模块
// @Accept json
// @Produce json
// @Param department body request.CreateDepartmentRequest true "部门信息"
// @Success 200 {object} response.Response{data=model.Department}
// @Router /departments [post]
func (h *DepartmentHandler) CreateDepartment(c *gin.Context) {
	var req request.CreateDepartmentRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, errors.NewBadRequestError("无效的请求参数", err))
		return
	}

	department, err := h.departmentService.CreateDepartment(&req)
	if err != nil {
		response.Error(c, err)
		return
	}

	response.SuccessWithMessage(c, "部门创建成功", department)
}

// UpdateDepartment godoc
// @Summary 更新部门
// @Description 更新指定部门信息
// @Tags 部门模块
// @Accept json
// @Produce json
// @Param id path uint true "部门ID"
// @Param department body request.UpdateDepartmentRequest true "部门信息"
// @Success 200 {object} response.Response{data=model.Department}
// @Router /departments/{id} [put]
func (h *DepartmentHandler) UpdateDepartment(c *gin.Context) {
	id, err := utils.ParseUintParam(c, "id")
	if err != nil {
		response.Error(c, errors.NewBadRequestError("无效的部门ID", err))
		return
	}

	var req request.UpdateDepartmentRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, errors.NewBadRequestError("无效的请求参数", err))
		return
	}

	department, err := h.departmentService.UpdateDepartment(id, &req)
	if err != nil {
		response.Error(c, err)
		return
	}

	response.SuccessWithMessage(c, "部门更新成功", department)
}

// DeleteDepartment godoc
// @Summary 删除部门
// @Description 删除指定部门
// @Tags 部门模块
// @Accept json
// @Produce json
// @Param id path uint true "部门ID"
// @Success 200 {object} response.Response
// @Router /departments/{id} [delete]
func (h *DepartmentHandler) DeleteDepartment(c *gin.Context) {
	id, err := utils.ParseUintParam(c, "id")
	if err != nil {
		response.Error(c, errors.NewBadRequestError("无效的部门ID", err))
		return
	}

	if err := h.departmentService.DeleteDepartment(id); err != nil {
		response.Error(c, err)
		return
	}

	response.SuccessWithMessage(c, "部门删除成功", nil)
}
